package cn.bopomo.dealer.base.db.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.bopomo.dealer.base.db.BaseDAO;
import cn.bopomo.dealer.base.db.IAuGroupDAO;
import cn.bopomo.dealer.base.exception.ExceptionCode;
import cn.bopomo.dealer.base.exception.SystemException;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuGroup;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;

public class AuGroupDAOImpl extends BaseDAO implements IAuGroupDAO {

	public String insertAuGroup(AuGroup group) {
		try {
			this.insertOrUpdate(group);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String updateAuGroup(AuGroup group) {
		try {
			this.insertOrUpdate(group);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String deleteAuGroup(AuGroup group) {
		try {
			this.getHibernateTemplate().delete(group);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public List selectGroups() throws SystemException {
		String hql = "from AuGroup";
		return this.selectAll(hql,null);
	}
	
	public List selectGroups(ModuleList module) throws SystemException {
		String hql = "from AuGroup where moduleList = ?";
		return this.selectAll(hql,new Object[]{module});
	}
	
	public List selectGroupsByAuthorize(String tag) throws SystemException {
		String hql = "from AuGroup where groupAuthorize like '%" + tag + "%'";
		
		return this.selectAll(hql, null);
	}

	public AuGroup selectGroup(Integer id) throws SystemException {
		String hql = "from AuGroup ";
		Object group = null;
		try {
			group = this.sectionFirstObject(hql,"where id = ?", id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (group != null){
			return (AuGroup)group;
		}
		return null;
	}

	public AuGroup selectGroup(String groupName, ModuleList module) throws SystemException {
		String hql = "from AuGroup ";
		Object group = null;
		List params = new ArrayList();
		params.add(groupName);
		params.add(module);
		try {
			group = this.sectionFirstObject(hql,"where groupName = ? and moduleList =?", params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (group != null){
			return (AuGroup)group;
		}
		return null;
	}

	public List selectGroups(final String idList) throws SystemException {
		final String sql = "from AuGroup where id in (" + idList + ")";
		
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(sql);
				
				//query.setParameter(0, idList);

				return query.list();
			}
		});
	}

}
